<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
import echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme

const animationDuration = 1000

export default {
  mixins: [],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '300px'
    }
  },
  data() {
    return {
      chart: null
    }
  },
  mounted() {
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart(title,vals) {
      this.chart = echarts.init(this.$el, 'macarons')

      this.chart.setOption({
         color: ['#348CE8'],
        tooltip: {
          trigger: 'axis',
          axisPointer: { // 坐标轴指示器，坐标轴触发有效
            type: 'shadow' // 默认为直线，可选为：'line' | 'shadow'
          }
        },
        grid: {
          top: 30,
          left: '2%',
          right: '2%',
          bottom: 60,
          containLabel: true
        },
        xAxis: [{
          type: 'category',
          data: title,
          axisTick: {
            alignWithLabel: true
          }
        }],
        yAxis: [{
          name: '单位/人',
          type: 'value',
          minInterval: 1, //不允许出现小数位
          axisTick: {
            show: false
          }
        }],
        series: [{
          name: '',
          type: 'bar',
          barWidth: '60%',
          data: vals,
          itemStyle: {
							normal: {
								label: {
									show: true, //开启显示
									position: 'top', //在上方显示
									textStyle: { //数值样式
										color: '#666666',
										fontSize: 16
									}
								}
							}
						},
          animationDuration
        }]
      })
    }
  }
}
</script>
